
/**
 * DataTables
 */
var datatable = null,idList=[];

$(function() {
	datatable = $('.table-sort').dataTable({
		order: [
			[1, 'asc']
		],
		bRetrieve: true,
		autoWidth: true,
		searching : false,
		lengthChange: false,	
		processing: true, //当datatable获取数据时候是否显示正在处理提示信息。
		serverSide: true, //服务器处理分页
		responsive: {
			details: false
		},
	    //lengthMenu: [100],   // 每页显示数据（默认是10）
	    ordering: false,//是否允许用户排序
	    paging: true,//是否分页
	    pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
	    /* scrollX: true,//允许水平滚动
	     scrollY: "200px",
	     scrollCollapse: true, */
	    stateSave: false,//刷新时是否保存状态
	    //deferRender: true,//延迟渲染
		ajax: {
			url: contextPath + "/issuePeriod/getPageList",
			type: 'post',
			data: function(d) {
				//d.search = $('#search').val();
			}
		},	
		language: {
			"url": "../resources/js/datatables_language.json"
		},
		columns: [
			{data: "issuePeriodId"},
			{data: "issuePeriodAlias",defaultContent: ""},
			{data: "issueStartTime",defaultContent: ""},
			{data: "issueEndTime",defaultContent: ""},
			{data: "salePrice",defaultContent: ""},
			{data: "validityStartTime",defaultContent: ""},
			{data: "validityEndTime",defaultContent: ""},
			{data: "saleRatio",defaultContent: ""},
			{data: "swipeTimes",defaultContent: ""},
			{data: "cardFacePic",defaultContent: ""},
			{data: "cardType",defaultContent: ""},
			{data: "publicState",defaultContent: ""},
			{data: "createTime",defaultContent: ""},
			{data: null}
			],
		columnDefs: [{
			targets: [0],
			orderable:false,
			render: function(data, type, row, meta) {
				return '<input id="input-' + data + '" type="checkbox" name="single"><label for="input-' + data + '"></label>';
			}  
		}, {
			targets: [2],
			render: function(data, type, row, meta) {
				return data ? new Date(data).pattern("yyyy-MM-dd") : ''; //"yyyy-MM-dd HH:mm:ss"
			}
		}, {
			targets: [3],
			render: function(data, type, row, meta) {
				return data ? new Date(data).pattern("yyyy-MM-dd") : '';
			}
		}, {
			targets: [5],
			render: function(data, type, row, meta) {
				return data ? new Date(data).pattern("yyyy-MM-dd") : '';
			}
		}, {
			targets: [6],
			render: function(data, type, row, meta) {
				return data ? new Date(data).pattern("yyyy-MM-dd") : '';
			}
		},  {
			targets: [9],
			render: function(data, type, row, meta) {
				return '<img width=\"100\" height=\"60\" class=\"picture-thumb\" src=\"/image/' + data + '\">';
;
			}
		}, {
			targets: [11],
			render: function(data, type, row, meta) {

				var clazz = new Array('primary', 'success', 'defaunt','danger');
				var text = new Array('待发布', '上架', '下架', '停用');
				return '<span class="label label-' + clazz[data] + ' radius">' + text[data] + '</span>';
			}
		}, {
			targets: [12],
			render: function(data, type, row, meta) {
				return data ? new Date(data).pattern("yyyy-MM-dd HH:mm:ss") : '';
			}
		},{
			targets: [13],
			orderable:false,
			responsivePriority: 1,
			render: function(data, type, row, meta) {
				var btns = new Array('review','edit','del');
				return getDTOperateBtn(btns, row.issuePeriodId, '/issuePeriod/issuePeriod-add.html', row.publicState, '/issuePeriod/delete', reloadTable);
			}
		}]
	});

});

function review(id, status) {
	switch(status) {
		case 0:
		case 2:
			layer.confirm('本期发布上架?', {
					btn: ['发布上架', '取消'],
					shade: false,
					closeBtn: 0
				},
				function() {
					changeStatus('/issuePeriod/changeStatus/', id, 1, reloadTable);
				});
			break;
		case 1:
			layer.confirm('确定停用?', {
					btn: ['停用', '取消'],
					shade: false,
					closeBtn: 0
				},
				function() {
					changeStatus('/issuePeriod/changeStatus/', id, 3, reloadTable);
				});
			break;
		case 3:
			layer.confirm('确定下架?', {
				    btn: ['下架', '取消'],
					shade: false,
					closeBtn: 0
				},
				function() {
					changeStatus('/issuePeriod/changeStatus/', id, 2, reloadTable);
				});
			break;
	};
}
